import 'package:get/get.dart';
import 'package:syn_4s/core/constants/constants.dart';

import '../model/base/api_result_data.dart';
import '../model/base/base_query_model.dart';
import '../model/base/paged_list.dart';

class BaseListControllerViewModel<D, Q extends BaseQueryModel>
    extends GetxController {
  final RxList<D> _data = <D>[].obs;
  late Q query;
  int totalPages = 0;
  int getDataType = 0;

  List<D> get data => _data;

  set data(List<D> value) {
    _data.addAll(value);
  }

  clearData() {
    _data.clear();
  }

  deleteData(int index) {
    _data.removeAt(index);
  }

  Future<ApiResultData<PagedList<D>>>? refreshData() {
    getDataType = refresh;
    clearData();
    query.pageIndex = 1;
    return loadData();
  }

  Future<ApiResultData<PagedList<D>>>? loadMoreData() {
    getDataType = loadMore;
    if (totalPages == 0) {
      totalPages = 1;
    }
    if (totalPages <= (query.pageIndex ?? 0)) {
      query.pageIndex = ++totalPages;
    } else {
      query.pageIndex = (query.pageIndex ?? 0) + 1;
    }
    return loadData();
  }

  Future<ApiResultData<PagedList<D>>>? loadData() {
    return null;
  }
}
